package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdSensitiveMapper;
import com.heima.admin.service.AdSensitiveService;
import com.heima.model.admin.dtos.SensitiveDTO;
import com.heima.model.admin.pojos.AdSensitive;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Transactional
@Service
public class AdSensitiveServiceImpl extends ServiceImpl<AdSensitiveMapper, AdSensitive> implements AdSensitiveService {

    @Override
    public ResponseResult fendList(SensitiveDTO dto) {
        //参数校验
        dto.checkParam();
        //根据名称分页查询
        IPage<AdSensitive> pageParam = new Page<>(dto.getPage(),dto.getSize());
        LambdaQueryWrapper<AdSensitive> wrapper = new LambdaQueryWrapper<>();
        //判断是否搜索
        if (StringUtils.isNotBlank(dto.getName())) {
            wrapper.like(AdSensitive::getSensitives, dto.getName());
        }
        IPage<AdSensitive> page = this.page(pageParam, wrapper);
        //返回结果
        PageResponseResult responseResult = new PageResponseResult(dto.getPage(), dto.getSize(), page.getTotal());
        responseResult.setData(page.getRecords());
        return responseResult;
    }

    @Override
    public ResponseResult addDto(AdSensitive adSensitive) {
        //参数校验
        if(adSensitive == null||StringUtils.isBlank(adSensitive.getSensitives())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //判断是否存在
        LambdaQueryWrapper<AdSensitive> wrapper = new LambdaQueryWrapper<>();
        int count = this.count(wrapper.eq(AdSensitive::getSensitives, adSensitive.getSensitives()));
        if(count > 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        //添加数据
        adSensitive.setCreatedTime(new Date());
        this.save(adSensitive);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult updateAd(AdSensitive adSensitive) {
        //参数校验
        if(adSensitive.getId() == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //检验修改名称是否存在
        LambdaQueryWrapper<AdSensitive> wrapper = new LambdaQueryWrapper<>();
        int count = this.count(wrapper.eq(AdSensitive::getSensitives, adSensitive.getSensitives()));
        if (count>0){
            //存在
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        //修改
        this.updateById(adSensitive);
        return ResponseResult.okResult();
    }

    @Override
    public ResponseResult deleteById(Integer id) {
        //参数校验
        if (id==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //判断参数是否有效
        AdSensitive adSensitive = this.getById(id);
        if (adSensitive==null){
            //删除数据不存在
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        //删除数据
        this.removeById(id);
        return ResponseResult.okResult();
    }

    @Autowired
    AdSensitiveMapper adSensitiveMapper;
    /**
     * 查询敏感词
     * @return
     */
    @Override
    public ResponseResult selectAllSensitives() {
        List<String> sensitives = adSensitiveMapper.findAllSensitives();
        return ResponseResult.okResult(sensitives);
    }

}
